import { defineStore } from 'pinia';
import { EditorSettings } from 'types/editor/settings';
import { useLocaleStoreWithOut } from '/@/store/modules/locale';
import { editInitNode } from '/@/views/config-center/bpmn-process/components/xmlDemo';

const localeStore = useLocaleStoreWithOut();
export const defaultSettings: EditorSettings = {
  language: localeStore.getLocale,
  processId: `Process_${new Date().getTime()}`,
  processName: `业务流程`,
  processEngine: 'flowable',
  paletteMode: 'rewrite',
  penalMode: 'custom',
  contextPadMode: 'enhancement',
  rendererMode: 'rewrite',
  bg: 'grid',
  toolbar: true,
  miniMap: true,
  contextmenu: true,
  customContextmenu: false,
  otherModule: true,
  templateChooser: false,
  useLint: false,
  customTheme: {},
  theme: 1,
};

interface bpmnInitNodeXMl {
  [key: number]: string;
}

const initEditBpmn: bpmnInitNodeXMl = editInitNode;

const state = {
  editorSettings: defaultSettings,
  initEditBpmn: initEditBpmn,
  bpmnXml: '',
};

export default defineStore('editor', {
  state: () => state,
  getters: {
    getLocales: (state) => state.editorSettings.language,
    getProcessDef: (state) => ({
      processName: state.editorSettings.processName,
      processId: state.editorSettings.processId,
    }),
    getProcessEngine: (state) => state.editorSettings.processEngine,
    getEditorConfig: (state) => ({
      bg: state.editorSettings.bg,
      paletteMode: state.editorSettings.paletteMode,
      penalMode: state.editorSettings.penalMode,
      contextPadMode: state.editorSettings.contextPadMode,
      rendererMode: state.editorSettings.rendererMode,
      toolbar: state.editorSettings.toolbar,
      miniMap: state.editorSettings.miniMap,
      contextmenu: state.editorSettings.contextmenu,
      customContextmenu: state.editorSettings.customContextmenu,
      otherModule: state.editorSettings.otherModule,
      templateChooser: state.editorSettings.templateChooser,
      useLint: state.editorSettings.useLint,
      customTheme: state.editorSettings.customTheme,
    }),
    getUpdateContent: (state) => state.bpmnXml,
    getInitEditBpmn: (state) => state.initEditBpmn,
  },
  actions: {
    updateConfiguration(conf: Partial<EditorSettings>) {
      sessionStorage.setItem('lang', <string>conf.language);
      this.$state.editorSettings = { ...this.$state.editorSettings, ...conf };
    },
    updateLanguage(lang: string) {
      sessionStorage.setItem('lang', lang);
      this.$state.editorSettings.language = lang || 'zh_CN';
    },
    updateTheme(theme: number) {
      this.$state.editorSettings.theme = theme;
    },
    updateBpmnInitNode(data: bpmnInitNodeXMl) {
      this.$state.initEditBpmn = data;
    },
    updateBpmnContent(content: string) {
      this.$state.bpmnXml = content;
    },
  },
});
